<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>小豆子的Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta property="og:type" content="website">
<meta property="og:title" content="小豆子的Blog">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="小豆子的Blog">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="小豆子的Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">小豆子的Blog</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main">
  
    <article id="post-Python第三方库numpy的简单入门使用" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/06/07/Python%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%93numpy%E7%9A%84%E7%AE%80%E5%8D%95%E5%85%A5%E9%97%A8%E4%BD%BF%E7%94%A8/" class="article-date">
  <time datetime="2020-06-07T07:08:45.000Z" itemprop="datePublished">2020-06-07</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/06/07/Python%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%93numpy%E7%9A%84%E7%AE%80%E5%8D%95%E5%85%A5%E9%97%A8%E4%BD%BF%E7%94%A8/">Python第三方库numpy的简单入门使用</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>首先，我们需要导入包文件</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br></pre></td></tr></table></figure>

<h2 id="一、numpy中的一般语法"><a href="#一、numpy中的一般语法" class="headerlink" title="一、numpy中的一般语法"></a>一、numpy中的一般语法</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 矩阵的逆、转置及秩</span></span><br><span class="line">data_1 = np.matrix(<span class="string">'3 4 5;7 8 9'</span>)</span><br><span class="line">print(data_1)</span><br><span class="line">print(<span class="string">"矩阵的逆:"</span>)</span><br><span class="line">data_2 = data_1.I</span><br><span class="line">print(data_2)</span><br><span class="line">print(<span class="string">"矩阵的转置:"</span>)</span><br><span class="line">data_3 = data_1.T</span><br><span class="line">print(data_3)</span><br><span class="line">print(<span class="string">"矩阵的秩:"</span>)</span><br><span class="line">data_4 = np.linalg.matrix_rank(data_1)</span><br><span class="line">print(data_4)</span><br></pre></td></tr></table></figure>

<pre><code>[[3 4 5]
 [7 8 9]]
矩阵的逆:
[[-1.08333333  0.58333333]
 [-0.08333333  0.08333333]
 [ 0.91666667 -0.41666667]]
矩阵的转置:
[[3 7]
 [4 8]
 [5 9]]
矩阵的秩:
2</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 矩阵的维度（也称为轴数）</span></span><br><span class="line">print(data_1.ndim)</span><br></pre></td></tr></table></figure>

<pre><code>2</code></pre><h2 id="二、数组的创建"><a href="#二、数组的创建" class="headerlink" title="二、数组的创建"></a>二、数组的创建</h2><ol>
<li>创建二维数组<br>  这里强调：使用matrix只能创建二维矩阵，而是用非matrix方法可以创建高维矩阵</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 全0的矩阵</span></span><br><span class="line">arr_1 = np.matrix(np.zeros((<span class="number">3</span>, <span class="number">3</span>)))</span><br><span class="line">print(arr_1)</span><br><span class="line"><span class="comment"># 或者</span></span><br><span class="line">arr_1 = np.zeros((<span class="number">3</span>, <span class="number">3</span>))</span><br><span class="line">print(arr_1)</span><br></pre></td></tr></table></figure>

<pre><code>[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 全1的矩阵</span></span><br><span class="line">arr_2 = np.matrix(np.ones((<span class="number">3</span>, <span class="number">3</span>)))</span><br><span class="line">print(arr_2)</span><br><span class="line"><span class="comment"># 或者</span></span><br><span class="line">arr_2 = np.ones((<span class="number">3</span>, <span class="number">3</span>))</span><br><span class="line">print(arr_2)</span><br></pre></td></tr></table></figure>

<pre><code>[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建随机矩阵</span></span><br><span class="line">arr_3 = np.matrix(np.random.rand(<span class="number">3</span>, <span class="number">4</span>))</span><br><span class="line">arr_3</span><br></pre></td></tr></table></figure>




<pre><code>matrix([[0.48890396, 0.09218043, 0.38339213, 0.61843015],
        [0.35092127, 0.08485116, 0.84651857, 0.4481831 ],
        [0.88727766, 0.56471182, 0.49473952, 0.69027512]])</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建整数随机矩阵</span></span><br><span class="line">arr_4 = np.matrix(np.random.randint(<span class="number">5</span>, <span class="number">10</span>, size=(<span class="number">3</span>, <span class="number">4</span>)))</span><br><span class="line">arr_4</span><br></pre></td></tr></table></figure>




<pre><code>matrix([[7, 7, 7, 5],
        [6, 5, 8, 5],
        [5, 6, 7, 6]])</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建对角矩阵</span></span><br><span class="line">new_list = [i <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">3</span>, <span class="number">7</span>)]</span><br><span class="line">arr_5 = np.matrix(np.diag(new_list))</span><br><span class="line">arr_5</span><br></pre></td></tr></table></figure>




<pre><code>matrix([[3, 0, 0, 0],
        [0, 4, 0, 0],
        [0, 0, 5, 0],
        [0, 0, 0, 6]])</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建单位矩阵</span></span><br><span class="line">arr_6 = np.eye(<span class="number">3</span>)</span><br><span class="line">arr_6</span><br></pre></td></tr></table></figure>




<pre><code>array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 改变矩阵的形状</span></span><br><span class="line">print(arr_4)</span><br><span class="line">arr_7 = arr_4.reshape(<span class="number">4</span>, <span class="number">3</span>)</span><br><span class="line">arr_7</span><br></pre></td></tr></table></figure>

<pre><code>[[7 7 7 5]
 [6 5 8 5]
 [5 6 7 6]]





matrix([[7, 7, 7],
        [5, 6, 5],
        [8, 5, 5],
        [6, 7, 6]])</code></pre><ol start="2">
<li>高维数组</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 例如创建3维的数组</span></span><br><span class="line">arr_8 = np.arange(<span class="number">24</span>).reshape(<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>)</span><br><span class="line">print(arr_8)</span><br><span class="line">print(<span class="string">"数组维度："</span>+str(arr_8.ndim))</span><br></pre></td></tr></table></figure>

<pre><code>[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
数组维度：3</code></pre><h2 id="三、numpy的索引和切片"><a href="#三、numpy的索引和切片" class="headerlink" title="三、numpy的索引和切片"></a>三、numpy的索引和切片</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">print(arr_8)</span><br><span class="line">print(<span class="string">"单独获取到一个值"</span>)</span><br><span class="line">print(arr_8[<span class="number">0</span>,<span class="number">0</span>,<span class="number">3</span>])</span><br></pre></td></tr></table></figure>

<pre><code>[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
单独获取到一个值
3</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">print(arr_8)</span><br><span class="line">print(<span class="string">"获取到深度为0的所有值"</span>)</span><br><span class="line">print(arr_8[<span class="number">0</span>,:,:])</span><br></pre></td></tr></table></figure>

<pre><code>[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
获取到深度为0的所有值
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">print(arr_8)</span><br><span class="line">print(<span class="string">"获取到深度为0的前两行的值"</span>)</span><br><span class="line">print(arr_8[<span class="number">0</span>,:<span class="number">2</span>,:])</span><br></pre></td></tr></table></figure>

<pre><code>[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
获取到深度为0的前两行的值
[[0 1 2 3]
 [4 5 6 7]]</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">print(arr_8)</span><br><span class="line">print(<span class="string">"获取到深度为0的前两行后两列的值"</span>)</span><br><span class="line">print(arr_8[<span class="number">0</span>,:<span class="number">2</span>,<span class="number">-2</span>:])</span><br></pre></td></tr></table></figure>

<pre><code>[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
获取到深度为0的前两行后两列的值
[[2 3]
 [6 7]]</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">print(arr_8)</span><br><span class="line">print(<span class="string">"获取到深度为0的前两行后两列的值并替换为-2"</span>)</span><br><span class="line">print(<span class="string">"获取到深度为0的前两行后两列的值"</span>)</span><br><span class="line">print(arr_8[<span class="number">0</span>,:<span class="number">2</span>,<span class="number">-2</span>:])</span><br><span class="line">print(<span class="string">"替换为-2"</span>)</span><br><span class="line">arr_8[<span class="number">0</span>,:<span class="number">2</span>,<span class="number">-2</span>:] = <span class="number">-2</span></span><br><span class="line">print(<span class="string">"原来数组"</span>)</span><br><span class="line">print(arr_8)</span><br></pre></td></tr></table></figure>

<pre><code>[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
获取到深度为0的前两行后两列的值并替换为-2
获取到深度为0的前两行后两列的值
[[2 3]
 [6 7]]
替换为-2
原来数组
[[[ 0  1 -2 -2]
  [ 4  5 -2 -2]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]</code></pre><h2 id="四、numpy的合并功能"><a href="#四、numpy的合并功能" class="headerlink" title="四、numpy的合并功能"></a>四、numpy的合并功能</h2><pre><code>1. 在第一轴合并
2. 在第二轴合并
3. 在第三轴合并
4. 在任意轴合并</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1. 在第一轴合并</span></span><br><span class="line"><span class="comment"># vstack</span></span><br><span class="line">print(<span class="string">"合并前的arr_8\n"</span>+str(arr_8))</span><br><span class="line">temp_1 = arr_8</span><br><span class="line">temp_2 = arr_8</span><br><span class="line">temp = np.vstack((temp_1, temp_2))</span><br><span class="line">print(<span class="string">"两个arr_8合并后\n"</span>+str(temp))</span><br></pre></td></tr></table></figure>

<pre><code>合并前的arr_8
[[[ 0  1 -2 -2]
  [ 4  5 -2 -2]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
两个arr_8合并后
[[[ 0  1 -2 -2]
  [ 4  5 -2 -2]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]

 [[ 0  1 -2 -2]
  [ 4  5 -2 -2]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 2. 在第二轴合并</span></span><br><span class="line"><span class="comment"># hstack</span></span><br><span class="line">print(<span class="string">"合并前的arr_8\n"</span>+str(arr_8))</span><br><span class="line">temp_1 = arr_8</span><br><span class="line">temp_2 = arr_8</span><br><span class="line">temp = np.hstack((temp_1, temp_2))</span><br><span class="line">print(<span class="string">"两个arr_8合并后\n"</span>+str(temp))</span><br></pre></td></tr></table></figure>

<pre><code>合并前的arr_8
[[[ 0  1 -2 -2]
  [ 4  5 -2 -2]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
两个arr_8合并后
[[[ 0  1 -2 -2]
  [ 4  5 -2 -2]
  [ 8  9 10 11]
  [ 0  1 -2 -2]
  [ 4  5 -2 -2]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]
  [12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 3. 在第三轴合并</span></span><br><span class="line"><span class="comment"># dstack</span></span><br><span class="line">print(<span class="string">"合并前的arr_8\n"</span>+str(arr_8))</span><br><span class="line">temp_1 = arr_8</span><br><span class="line">temp_2 = arr_8</span><br><span class="line">temp = np.dstack((temp_1, temp_2))</span><br><span class="line">print(<span class="string">"两个arr_8合并后\n"</span>+str(temp))</span><br></pre></td></tr></table></figure>

<pre><code>合并前的arr_8
[[[ 0  1 -2 -2]
  [ 4  5 -2 -2]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
两个arr_8合并后
[[[ 0  1 -2 -2  0  1 -2 -2]
  [ 4  5 -2 -2  4  5 -2 -2]
  [ 8  9 10 11  8  9 10 11]]

 [[12 13 14 15 12 13 14 15]
  [16 17 18 19 16 17 18 19]
  [20 21 22 23 20 21 22 23]]]</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 4. 任意轴合并</span></span><br><span class="line"><span class="comment"># concatenate</span></span><br><span class="line">print(<span class="string">"合并前的arr_8\n"</span>+str(arr_8))</span><br><span class="line">temp_1 = arr_8</span><br><span class="line">temp_2 = arr_8</span><br><span class="line"><span class="comment"># axis表示设置在何处轴合并，默认在第一轴</span></span><br><span class="line">temp = np.concatenate((arr_8, arr_8), axis=<span class="number">2</span>)</span><br><span class="line">print(<span class="string">"两个arr_8合并后\n"</span>+str(temp))</span><br></pre></td></tr></table></figure>

<pre><code>合并前的arr_8
[[[ 0  1 -2 -2]
  [ 4  5 -2 -2]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
两个arr_8合并后
[[[ 0  1 -2 -2  0  1 -2 -2]
  [ 4  5 -2 -2  4  5 -2 -2]
  [ 8  9 10 11  8  9 10 11]]

 [[12 13 14 15 12 13 14 15]
  [16 17 18 19 16 17 18 19]
  [20 21 22 23 20 21 22 23]]]</code></pre>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/06/07/Python%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%93numpy%E7%9A%84%E7%AE%80%E5%8D%95%E5%85%A5%E9%97%A8%E4%BD%BF%E7%94%A8/" data-id="ckb4q7iel00005cbigcmm0ib7" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-Python迭代器及生成器的简单理解" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/06/02/Python%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%8F%8A%E7%94%9F%E6%88%90%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/" class="article-date">
  <time datetime="2020-06-02T00:36:31.000Z" itemprop="datePublished">2020-06-02</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/06/02/Python%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%8F%8A%E7%94%9F%E6%88%90%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/">Python迭代器及生成器的简单理解</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="迭代器"><a href="#迭代器" class="headerlink" title="迭代器"></a>迭代器</h2><p>&emsp;&emsp;在使用列表时，直接将数据存入列表将会占据大量空间，且复用率较低，<br>为解决这个问题，这里了解一下迭代器，从而创建一种数据产生的方式，以此来节省空间。</p>
<h2 id="迭代对象"><a href="#迭代对象" class="headerlink" title="迭代对象"></a>迭代对象</h2><p>&emsp;&emsp;注意，这里需要使用到内建函数<strong>iter</strong>，简单理解为，使用了<strong>iter</strong>才会是一个可迭代对象，关于这部分，我们可以对一些对象做一些判断，从而清楚是不是可迭代对象，比如L1列表：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">from collections.abc import Iterable</span><br><span class="line"># 判断L1是否为可迭代对象</span><br><span class="line">L1 &#x3D; [i for i in range(4)]</span><br><span class="line">print(isinstance(L1, Iterable)) # 结果为True，确定为迭代对象</span><br></pre></td></tr></table></figure>
<p>&emsp;&emsp;这里我们使用<strong>iter</strong>()方法和<strong>next</strong>()方法来将元素取出。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">L2 &#x3D; L1.__iter__()</span><br><span class="line"># L2 &#x3D; iter(L1) # 和上面一句等价</span><br><span class="line">print(next(L2))</span><br><span class="line">print(L2.__next__()) # 和上面一句等价</span><br><span class="line">print(next(L2))</span><br></pre></td></tr></table></figure>
<p>&emsp;&emsp;在这里我们实现了使用for循环来取出元素的方法，在了解这里之后，我们开始做一个自己的迭代器</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"># 构造迭代器</span><br><span class="line">class NewIter():</span><br><span class="line">    # 用于生成斐波那契数列</span><br><span class="line">    def __init__(self, num):</span><br><span class="line">        self.num &#x3D; num</span><br><span class="line">        self.a &#x3D; 0</span><br><span class="line">        self.b &#x3D; 1</span><br><span class="line"></span><br><span class="line">    def __iter__(self):</span><br><span class="line">        # 创建可迭代对象</span><br><span class="line">        return self</span><br><span class="line">    </span><br><span class="line">    def __next__(self):</span><br><span class="line">        while self.num &gt; 0:</span><br><span class="line">            result &#x3D; self.a</span><br><span class="line">            self.a, self.b &#x3D; self.b, self.a + self.b</span><br><span class="line">            self.num -&#x3D; 1</span><br><span class="line">            return result</span><br><span class="line">        # 循环结束后跳出</span><br><span class="line">        raise StopIteration</span><br><span class="line">        </span><br><span class="line">new_iter &#x3D; NewIter(5)</span><br><span class="line">print(next(new_iter))</span><br><span class="line">print(next(new_iter))</span><br></pre></td></tr></table></figure>
<h2 id="生成器"><a href="#生成器" class="headerlink" title="生成器"></a>生成器</h2><p>&emsp;&emsp;这里引入生成器个概念，生成器能做到迭代器能做的所有事，由于自动创建了iter()和next()方法，生成器显得特别简洁。除了创建和保存程序状态的自动方法,当<br>发生器终结时,还会自动抛出 StopIteration 异常。简单理解他就是迭代器的一种！！使用yield即可实现。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"># 生成器实现</span><br><span class="line">def new_create(num):</span><br><span class="line">    # 用于生成斐波那契数列</span><br><span class="line">    a, b &#x3D; 0, 1</span><br><span class="line">    while num &gt; 0:</span><br><span class="line">        yield a</span><br><span class="line">        a, b &#x3D; b, a+b</span><br><span class="line">        num -&#x3D; 1</span><br><span class="line">        </span><br><span class="line">new_iter &#x3D; new_create(5)</span><br><span class="line">print(next(new_iter))</span><br><span class="line">print(next(new_iter))</span><br></pre></td></tr></table></figure>
<h2 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h2><p>&emsp;&emsp;目的就是节省空间，提高复用率</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/06/02/Python%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%8F%8A%E7%94%9F%E6%88%90%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/" data-id="ckax72g0g00006bbib25q14dw" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-Python装饰器的简单理解" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/06/01/Python%E8%A3%85%E9%A5%B0%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/" class="article-date">
  <time datetime="2020-06-01T07:46:14.000Z" itemprop="datePublished">2020-06-01</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/06/01/Python%E8%A3%85%E9%A5%B0%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/">Python装饰器的简单理解</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="装饰器"><a href="#装饰器" class="headerlink" title="装饰器"></a>装饰器</h2><p>&emsp;&emsp;通俗点理解就是锦上添花，关于这块需要参考一些软件开发的设计模式，在这里引入一个小的故事。<br>&emsp;&emsp;公司招了一个人A，某一天，项目组想要给一个接口添加一些时间计算，具体时间计算没有说，但是这个功能要加，A呢就去查看接口中的代码，在代码中添加了一个时间计算，过了两天，项目组要求所有接口全部添加时间计算，此时A则一个个的去每个接口中添加时间计算，以至于天天晚上加班，结果没过多久就被炒鱿鱼了。此时来了B，他先写了个时间计算函数并在每个接口头部添加这个函数名，实现了对应的加时间计算功能。没多久就升职加薪了。<br>&emsp;&emsp;看到这里，不禁会想，为什么呢？？？其实这里涉及到一个软件开发的设计模式-开闭原则（简单说就是功能可以加，代码修改是不允许的），A就明显犯了这个错误，<br>而B则是使用到了我们接下来要说到的装饰器。</p>
<h2 id="引入"><a href="#引入" class="headerlink" title="引入"></a>引入</h2><p>&emsp;&emsp;在了解装饰器之前，请先了解闭包的相关概念！！以及高阶函数的概念！！<br>这里我们有一个阶乘功能的函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">def product(value):</span><br><span class="line">    # 实现阶乘功能</span><br><span class="line">    result &#x3D; 1</span><br><span class="line">    while value &gt; 0:</span><br><span class="line">        result *&#x3D; value</span><br><span class="line">        value -&#x3D; 1</span><br><span class="line">    print(result)</span><br></pre></td></tr></table></figure>
<ol>
<li>我们需要添加一个计算运行时间的功能，请记住，不要改动product函数，直接做功能添加，这里使用装饰器处理。首先我们创建一个装饰器： <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">import time</span><br><span class="line"></span><br><span class="line">def count_time(func):</span><br><span class="line">    def call_func(var):</span><br><span class="line">        t1 &#x3D; time.time()</span><br><span class="line">        func(var)</span><br><span class="line">        t2 &#x3D; time.time()</span><br><span class="line">        print(&#39;消耗时间为：&#39;+ str(t2-t1))</span><br><span class="line">    return call_func</span><br></pre></td></tr></table></figure></li>
<li>对原有函数进行装饰改造 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">@count_time</span><br><span class="line">def product(value):</span><br><span class="line">    # 实现阶乘功能</span><br><span class="line">    result &#x3D; 1</span><br><span class="line">    while value &gt; 0:</span><br><span class="line">        result *&#x3D; value</span><br><span class="line">        value -&#x3D; 1</span><br><span class="line">    print(result)</span><br></pre></td></tr></table></figure></li>
<li>细节理解<br>&emsp;&emsp;在第二步中的@count_time其实就是装饰器的基本使用，我们在使用product(100)时，解释器不会直接去调用product函数，而是实现调用count_time功能，并将返回的call_func返回给product，也就是说此时的product其实是指向call_func的，当执行product（100）时，其实是在向call_func传递参数，实现功能，简单理解可以使用以下两行代码来表示： <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">product &#x3D; count_time(product) # 此时的product是指向call_func函数的</span><br><span class="line">product(100) # 相当于执行call_func(100)</span><br></pre></td></tr></table></figure>
<h2 id="更高一级的操作"><a href="#更高一级的操作" class="headerlink" title="更高一级的操作"></a>更高一级的操作</h2></li>
<li>原函数需要传递多个参数<br>&emsp;&emsp;这里很好处理，很正常函数传递参数相同，我们在装饰器中最内层函数中需要传递的变量名改为<code>*args, **kwargs</code>，在执行时，将其直接传入即可。</li>
<li>原函数有返回值<br>&emsp;&emsp;这里我们可以直接使用return将装饰器中最内层的执行函数部分return出去即可，比如： <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">def count_time(func):</span><br><span class="line">    def call_func(var):</span><br><span class="line">        return func(var)</span><br><span class="line">    return call_func</span><br></pre></td></tr></table></figure></li>
<li>两个装饰器同时装饰同一个函数<br>&emsp;&emsp;先看一下伪代码 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">@装饰器1</span><br><span class="line">@装饰器2</span><br><span class="line">def fun():</span><br><span class="line">    pass</span><br></pre></td></tr></table></figure>
&emsp;&emsp;意思很简单，就是在原来函数基础上加两个新功能，但是关于执行顺序是怎么样的呢？<br>&emsp;&emsp;就不买关子了，装饰器1先执行，但装饰器1最后装饰，其实这里和堆栈很相似，若是方便，可以自己编代码，打断点调试一下就知道了。<h2 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h2>未完待续…</li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/06/01/Python%E8%A3%85%E9%A5%B0%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/" data-id="ckaw6zcnf00006ebie44x01oy" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-Python闭包的简要介绍" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/06/01/Python%E9%97%AD%E5%8C%85%E7%9A%84%E7%AE%80%E8%A6%81%E4%BB%8B%E7%BB%8D/" class="article-date">
  <time datetime="2020-06-01T05:23:17.000Z" itemprop="datePublished">2020-06-01</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/06/01/Python%E9%97%AD%E5%8C%85%E7%9A%84%E7%AE%80%E8%A6%81%E4%BB%8B%E7%BB%8D/">闭包的简要介绍</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="闭包"><a href="#闭包" class="headerlink" title="闭包"></a>闭包</h2><ul>
<li>是内层函数对外层函数非全局变量的应用</li>
<li>闭包会一直存在计算机内存中，不会因为函数执行的结束而释放</li>
</ul>
<h2 id="举例说明"><a href="#举例说明" class="headerlink" title="举例说明"></a>举例说明</h2><p>&emsp;&emsp;这里我们做一个求和函数，但是我们需要做到每次求和后也返回一个平均值及对应个数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">def make_averager():</span><br><span class="line">    count &#x3D; 0</span><br><span class="line">    total &#x3D; 0</span><br><span class="line"></span><br><span class="line">    def fun(var):</span><br><span class="line">        # nonlocal count, total # 这里不加会报错</span><br><span class="line">        count +&#x3D; 1</span><br><span class="line">        total +&#x3D; var</span><br><span class="line">        print(total)</span><br><span class="line">        print(str(total&#x2F;count) + &#39; , &#39; + str(count))</span><br><span class="line">    return fun</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">if __name__ &#x3D;&#x3D; &quot;__main__&quot;:</span><br><span class="line">    # 这里用到了高阶函数的定义，简单说就是可以将函数的返回值存储在变量中使用</span><br><span class="line">    average &#x3D; make_averager()</span><br><span class="line">    average(5)</span><br><span class="line">    average(6)</span><br><span class="line">    average(7)</span><br></pre></td></tr></table></figure>
<p>&emsp;&emsp;很糟糕，这里会有一个报错UnboundLocalError: local variable ‘count’ referenced before assignment，关于这里其实和变量的使用范围有关，在fun函数内部使用关键字nonlocal添加nonlocal count, total即可解决。<br>&emsp;&emsp;nonlocal声明的变量不是局部变量，也不是全局变量，而是外部嵌套函数内的变量。<br>&emsp;&emsp;简单来说就是在修改函数外部的变量，需要使用global；但是在闭包中，修改外层函数中的变量则需要时使用到nonlocal。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/06/01/Python%E9%97%AD%E5%8C%85%E7%9A%84%E7%AE%80%E8%A6%81%E4%BB%8B%E7%BB%8D/" data-id="ckaw6zco000016ebia2q21oxq" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-Python连接C实现高效编程demo" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/05/31/Python%E8%BF%9E%E6%8E%A5C%E5%AE%9E%E7%8E%B0%E9%AB%98%E6%95%88%E7%BC%96%E7%A8%8Bdemo/" class="article-date">
  <time datetime="2020-05-31T00:54:12.000Z" itemprop="datePublished">2020-05-31</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/05/31/Python%E8%BF%9E%E6%8E%A5C%E5%AE%9E%E7%8E%B0%E9%AB%98%E6%95%88%E7%BC%96%E7%A8%8Bdemo/">Python连接C实现高效编程demo</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="一、前言"><a href="#一、前言" class="headerlink" title="一、前言"></a>一、前言</h2><p>作为一门经典的编程语言，这些年来，C有着重要的地位，在各个领域的使用都是相当重要的地位！而Python作为较新式的语言，相对而言入门更加简单，适合新手来学习！但是作为一门动态的脚本语言，其效率相对C来说还是有些捉急的。在这里，我们引入一个demo来作为Python和C混合编程的一个基础。</p>
<h2 id="二、编写代码"><a href="#二、编写代码" class="headerlink" title="二、编写代码"></a>二、编写代码</h2><p>在这里我会使用对比的方式来比较两种语言计算一个较大数字求和时的耗时情况，从而直观体验其中的差别。同时这里会引入混合编程的概念。   </p>
<ol>
<li><p>编写C的代码</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">#include&lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">void add(long VALUE)&#123;</span><br><span class="line">    long sum &#x3D; 0;</span><br><span class="line">    for (long i &#x3D; 1;i &lt; VALUE; i ++)</span><br><span class="line">        sum +&#x3D; i;</span><br><span class="line">    printf(&quot;C求和结果为: %ld\n&quot;, sum);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>编写Python代码</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">import time</span><br><span class="line"></span><br><span class="line">VALUE &#x3D; 9999999</span><br><span class="line"></span><br><span class="line">t1 &#x3D; time.time()</span><br><span class="line">sum &#x3D; 0</span><br><span class="line">for i in range(1, VALUE):</span><br><span class="line">    sum +&#x3D; i</span><br><span class="line">print(&#39;for循环求和结果为：&#39;+ str(sum))</span><br><span class="line">t2 &#x3D; time.time()</span><br><span class="line">print(&#39;for循环求和开销时间为：&#39;+ str(t2-t1))</span><br></pre></td></tr></table></figure></li>
<li><p>将之前的C代码编译成Python可以读取调用的文件</p>
<ul>
<li>gcc -fPIC -shared C的代码 -o 后续调用文件名.so<br>例如: gcc -fPIC -shared sum.c -o csum.so</li>
</ul>
</li>
<li><p>将csum.so加载到Python中，进行编写</p>
<ul>
<li>导入库文件<code>from ctypes import cdll</code></li>
<li>调用文件  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">result &#x3D; cdll.LoadLibrary(&quot;.&#x2F;csum.so&quot;)</span><br><span class="line">result.add(VALUE)</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>最终Python代码sum.py是酱紫</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">import time</span><br><span class="line">from ctypes import cdll</span><br><span class="line"></span><br><span class="line">VALUE &#x3D; 9999999</span><br><span class="line"></span><br><span class="line"># 使用Python求和</span><br><span class="line">t1 &#x3D; time.time()</span><br><span class="line">sum &#x3D; 0</span><br><span class="line">for i in range(1, VALUE):</span><br><span class="line">    sum +&#x3D; i</span><br><span class="line">print(&#39;for循环求和结果为：&#39;+ str(sum))</span><br><span class="line">t2 &#x3D; time.time()</span><br><span class="line">print(&#39;for循环求和开销时间为：&#39;+ str(t2-t1))</span><br><span class="line">print(&#39;-&#39;*30)</span><br><span class="line"></span><br><span class="line"># Python连接C求和</span><br><span class="line">t1 &#x3D; time.time()</span><br><span class="line">result &#x3D; cdll.LoadLibrary(&quot;.&#x2F;csum.so&quot;)</span><br><span class="line">result.add(VALUE)</span><br><span class="line">t2 &#x3D; time.time()</span><br><span class="line">print(&#39;使用C求和开销时间为：&#39;+ str(t2-t1))</span><br></pre></td></tr></table></figure></li>
<li><p>最终结果</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">for循环求和结果为：49999985000001</span><br><span class="line">for循环求和开销时间为：3.156290292739868</span><br><span class="line">------------------------------</span><br><span class="line">C求和结果为: 49999985000001</span><br><span class="line">使用C求和开销时间为：0.043367624282836914</span><br></pre></td></tr></table></figure>
<h2 id="三、结语"><a href="#三、结语" class="headerlink" title="三、结语"></a>三、结语</h2><p>由上述可以看出来，同样使用for循环，但是使用c的效率是非常非常高的，所以针对一些特殊场景，使用c编写，然后用Python调用也是不错的方法。</p>
</li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/05/31/Python%E8%BF%9E%E6%8E%A5C%E5%AE%9E%E7%8E%B0%E9%AB%98%E6%95%88%E7%BC%96%E7%A8%8Bdemo/" data-id="ckaucrn9h000056biakx6ec3p" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-使用高德地图API创建一个属于自己的天气闹钟" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/05/30/%E4%BD%BF%E7%94%A8%E9%AB%98%E5%BE%B7%E5%9C%B0%E5%9B%BEAPI%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%B1%9E%E4%BA%8E%E8%87%AA%E5%B7%B1%E7%9A%84%E5%A4%A9%E6%B0%94%E9%97%B9%E9%92%9F/" class="article-date">
  <time datetime="2020-05-30T14:44:31.722Z" itemprop="datePublished">2020-05-30</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/05/30/%E4%BD%BF%E7%94%A8%E9%AB%98%E5%BE%B7%E5%9C%B0%E5%9B%BEAPI%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%B1%9E%E4%BA%8E%E8%87%AA%E5%B7%B1%E7%9A%84%E5%A4%A9%E6%B0%94%E9%97%B9%E9%92%9F/">使用高德地图API创建一个属于自己的天气闹钟</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>本着是使用树莓派做一个个人天气预报的，通过百度也看到很多大神使用各种各样的方法来实现，本着简单、免费的想法，我去弄了个高德的开发者账号（ps:其实就是懒，哈哈哈哈哈），这个账号认证后每天有    300000次，其实也够我们自己去使用了，哈哈哈哈！后面关于文本合成语音的部分，打算接入阿里、讯飞的api的，emmm有点贵，算了，使用第三方库讲究着用吧（ps:声音确实是好僵硬）！</p>
<h2 id="准备"><a href="#准备" class="headerlink" title="准备"></a>准备</h2><ol>
<li>在这里，我们需要提前安装好requests库、pyttsx3库。<br> <code>pip3 install requests</code><br> <code>pip3 install pyttsx3</code></li>
<li>进入高德开发者平台，注册账号，按照流程创建一个应用（是web服务的api），此时就可以获取到一个<code>key</code>值，这个值在后面会用到，记得保存一下。   </li>
<li>打开高德的api使用文档，好了，开始撸代码…<h2 id="开始撸代码"><a href="#开始撸代码" class="headerlink" title="开始撸代码"></a>开始撸代码</h2>这里我们有三个任务   </li>
</ol>
<ul>
<li>获取当前地理位置   </li>
<li>根据当前的地理位置来获取天气情况</li>
<li>将得到的文本数据使用语音合成出来</li>
</ul>
<ol>
<li>首先我们来看如何获取地理位置信息</li>
</ol>
<ul>
<li><p>这里我们可以使用web服务的api中的IP查询来获取到对应的城市名和城市的adcode编码</p>
</li>
<li><p>这里我们可以看下这张表</p>
  <div class="md-atomic"><div class="table-container" style="width:100%;"><table class="md-table  " data-meta="%7B%22width%22%3A%22100%25%22%7D"><tbody><tr><th colspan="2"><p>参数名</p></th><th><p>含义</p></th><th><p>规则说明</p></th><th><p>是否必须</p></th><th><p>缺省值</p></th></tr><tr><td colspan="2" style="white-space: nowrap;"><p>key</p></td><td><p>请求服务权限标识</p></td><td><p>用户在高德地图官网申请Web服务API类型KEY</a></p></td><td><p>必填</p></td><td><p>无</p></td></tr><tr><td colspan="2" style="white-space: nowrap;"><p>ip</p></td><td><p>ip地址</p></td><td><p>需要搜索的IP地址（仅支持国内）</p><p>若用户不填写IP，则取客户http之中的请求来进行定位</p></td><td><p>可选</p></td><td><p>无</p></td></tr><tr><td colspan="2" style="white-space: nowrap;"><p>sig</p></td><td><p>签名</p></td><td><p>选择数字签名认证的付费用户必填</p></td><td><p>可选</p></td><td><p>无</p></td></tr><tr><td colspan="2" style="white-space: nowrap;"><p>output</p></td><td><p>返回格式</p></td><td><p>可选值：JSON,XML</p></td><td><p>可选</p></td><td><p>JSON</p></td></tr></tbody></table></div></div>

<hr>
  <div class="md-atomic"><div class="table-container" style="width:100%;"><table class="md-table  " data-meta="%7B%22width%22%3A%22100%25%22%7D"><tbody><tr><th colspan="3"><p>名称</p></th><th><p>含义</p></th><th><p>规则说明</p></th></tr><tr><td colspan="3" style="white-space: nowrap;"><p>status</p></td><td><p>返回结果状态值</p></td><td><p>值为0或1,0表示失败；1表示成功</p></td></tr><tr><td colspan="3" style="white-space: nowrap;"><p>info</p></td><td><p>返回状态说明</p></td><td><p>返回状态说明，status为0时，info返回错误原因，否则返回“OK”。</p></td></tr><tr><td colspan="3" style="white-space: nowrap;"><p>infocode</p></td><td><p>状态码</p></td><td><p>返回状态说明,10000代表正确,详情参阅info状态表</p></td></tr><tr><td colspan="3" style="white-space: nowrap;"><p>province</p></td><td><p>省份名称</p></td><td><p>若为直辖市则显示直辖市名称；</p><p>如果在局域网 IP网段内，则返回“局域网”；</p><p>非法IP以及国外IP则返回空</p></td></tr><tr><td colspan="3" style="white-space: nowrap;"><p>city</p></td><td><p>城市名称</p></td><td><p>若为直辖市则显示直辖市名称；</p><p>如果为局域网网段内IP或者非法IP或国外IP，则返回空</p></td></tr><tr><td colspan="3" style="white-space: nowrap;"><p>adcode</p></td><td><p data-spm-anchor-id="0.0.0.i2.25166ec7hbUfrG">城市的adcode编码</p></td><td><p><br></p></td></tr><tr><td colspan="3" style="white-space: nowrap;"><p>rectangle</p></td><td data-spm-anchor-id="0.0.0.i3.25166ec7hbUfrG"><p>所在城市矩形区域范围</p></td><td><p>所在城市范围的左下右上对标对</p></td></tr></tbody></table></div></div>

<p>  我们可以在使用时直接请求，此时可以获取到城市的adcode,那么我们直接调用返回结果即可。</p>
  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">def get_citycode():</span><br><span class="line"># 获取当前位置编码</span><br><span class="line">url &#x3D; &#39;https:&#x2F;&#x2F;restapi.amap.com&#x2F;v3&#x2F;ip?&#39;</span><br><span class="line">word &#x3D; None</span><br><span class="line">try:</span><br><span class="line">    r &#x3D; requests.get(url, params&#x3D;set_params(word))</span><br><span class="line">    text &#x3D; r.json()</span><br><span class="line">    adcode &#x3D; text[&#39;adcode&#39;]</span><br><span class="line">finally:</span><br><span class="line">    return adcode</span><br></pre></td></tr></table></figure></li>
</ul>
<ol start="2">
<li>获取地区天气情况</li>
</ol>
<ul>
<li>这里我们可以使用web服务的api中的天气查询来获取到对应的城市的天气，在api使用说明中明确表示，需要传入的参数至少有adcode哦！！好在我们之前已经获取到了。 </li>
<li>开始请求数据，并处理返回的数据，这里我就取几项数据，具体可以自定义。  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">def get_weather():</span><br><span class="line"># 获取当前位置天气</span><br><span class="line">url &#x3D; &#39;https:&#x2F;&#x2F;restapi.amap.com&#x2F;v3&#x2F;weather&#x2F;weatherInfo?&#39;</span><br><span class="line">word &#x3D; get_citycode()</span><br><span class="line">weather_dict &#x3D; &#123;&#125;</span><br><span class="line">try:</span><br><span class="line">    r &#x3D; requests.get(url, params&#x3D;set_params(word))</span><br><span class="line">    text &#x3D; r.json()</span><br><span class="line">    if text[&#39;info&#39;] &#x3D;&#x3D; &#39;OK&#39;:</span><br><span class="line">        weather_dict[&#39;-&#39;] &#x3D; text[&#39;lives&#39;][0][&#39;city&#39;]</span><br><span class="line">        weather_dict[&#39;天气&#39;] &#x3D; text[&#39;lives&#39;][0][&#39;weather&#39;]</span><br><span class="line">        weather_dict[&#39;温度&#39;] &#x3D; text[&#39;lives&#39;][0][&#39;temperature&#39;]</span><br><span class="line">        weather_dict[&#39;空气湿度&#39;] &#x3D; text[&#39;lives&#39;][0][&#39;humidity&#39;]</span><br><span class="line">        weather_dict[&#39;风向&#39;] &#x3D; text[&#39;lives&#39;][0][&#39;winddirection&#39;]</span><br><span class="line">        weather_dict[&#39;风力级别&#39;] &#x3D; text[&#39;lives&#39;][0][&#39;windpower&#39;]</span><br><span class="line">finally:</span><br><span class="line">    return weather_dict</span><br></pre></td></tr></table></figure></li>
</ul>
<ol start="3">
<li>开始语音合成处理<br>在这一步，需要使用到pyttsx3，查阅技术文档后，直接使用即可（ps:反正语音不是很好听，也就不做过多解释了，直接上代码） <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">def read_data():</span><br><span class="line">    # 阅读获取的文本</span><br><span class="line">    weather &#x3D; get_weather()</span><br><span class="line">    engine &#x3D; pyttsx3.init()</span><br><span class="line">    rate &#x3D; engine.getProperty(&#39;rate&#39;)</span><br><span class="line">    engine.setProperty(&#39;rate&#39;, rate-10)</span><br><span class="line">    engine.say(weather)</span><br><span class="line">    engine.runAndWait()</span><br></pre></td></tr></table></figure></li>
<li>补充，关于代码中set_params函数部分，可以这样写就行 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">def set_params(word):</span><br><span class="line"># 设置请求表单</span><br><span class="line">if word &#x3D;&#x3D; None:</span><br><span class="line">    params &#x3D; &#123;</span><br><span class="line">        &#39;key&#39;:你自己的key,</span><br><span class="line">    &#125;</span><br><span class="line">else:</span><br><span class="line">    params &#x3D; &#123;</span><br><span class="line">        &#39;key&#39;:你自己的key,</span><br><span class="line">        &#39;city&#39;:word,</span><br><span class="line">        # base是获取当前天气，而all可以返回预报天气，哈哈哈自己慢慢玩。</span><br><span class="line">        &#39;extensions&#39;:&#39;base&#39;,</span><br><span class="line">    &#125;</span><br><span class="line">return params</span><br></pre></td></tr></table></figure>
<h2 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h2>想不到吧，这么快就结束了！！嘿嘿嘿嘿嘿，将代码移植到树莓派上，再写一个自动化脚本，就完事啦。</li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/05/30/%E4%BD%BF%E7%94%A8%E9%AB%98%E5%BE%B7%E5%9C%B0%E5%9B%BEAPI%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%B1%9E%E4%BA%8E%E8%87%AA%E5%B7%B1%E7%9A%84%E5%A4%A9%E6%B0%94%E9%97%B9%E9%92%9F/" data-id="ckatqt44f0000clbi40c3fe1d" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-迁移Hexo操作" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/05/29/%E8%BF%81%E7%A7%BBHexo%E6%93%8D%E4%BD%9C/" class="article-date">
  <time datetime="2020-05-29T03:22:14.987Z" itemprop="datePublished">2020-05-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/05/29/%E8%BF%81%E7%A7%BBHexo%E6%93%8D%E4%BD%9C/">迁移Hexo操作</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>有时候我们可能要在另一台电脑上使用我们的博客平台，这时，需要我们重新再操作一个远程仓库，然后在创建文章吗？？？<br>很明显，这是不现实的，我们有什么办法解决这个问题呢？，有的，我们可以做一个远程仓库备份就可以了，这样我们下次使用只要配置好环境，直接clone下来就可以使用了，来，先看步骤！</p>
<ol>
<li><p>在GitHub上创建远程分支（都是点点点的操作）</p>
</li>
<li><p>配置环境</p>
<ul>
<li>Git</li>
<li>Node.js</li>
<li>Hexo<ul>
<li><code>sudo npm install hexo-cli -g</code></li>
</ul>
</li>
</ul>
</li>
<li><p><code>clone</code>仓库（这里有好几步）</p>
<ul>
<li>克隆下来后，再创建一下分支<code>git checkout -b dev</code>,</li>
<li>删除掉<code>.git</code>以外的文件，务必要这样做。注意：不是删除掉<code>.git</code>。</li>
<li>然后将<code>Blog</code>文件夹下的所有文件移到这个目录，并使用<code>git</code>操作一下，操作如下：  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git add .</span><br><span class="line">git commit -m &#39;&#39;</span><br><span class="line">git push --set-upstream origin dev</span><br></pre></td></tr></table></figure></li>
<li>完成到这里就结束了这些部分的操作</li>
<li>然后这样一下<code>npm install</code> （可能不需要）</li>
</ul>
</li>
<li><p>开始正常写文章</p>
<ul>
<li>重复{Hexo配合GitHub搭建个人博客}里面的5～6步骤就可以了。</li>
</ul>
</li>
<li><p>最后一步</p>
<ul>
<li>写完之后，得同步到dev分支一下奥  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git add .</span><br><span class="line">git commit -m &#39;&#39;</span><br><span class="line">git push origin dev</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/05/29/%E8%BF%81%E7%A7%BBHexo%E6%93%8D%E4%BD%9C/" data-id="ckarn4zr600023cbi2cz81qpo" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-Java/学习Java第二课" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/05/29/Java/%E5%AD%A6%E4%B9%A0Java%E7%AC%AC%E4%BA%8C%E8%AF%BE/" class="article-date">
  <time datetime="2020-05-29T02:53:56.202Z" itemprop="datePublished">2020-05-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/05/29/Java/%E5%AD%A6%E4%B9%A0Java%E7%AC%AC%E4%BA%8C%E8%AF%BE/">Java/学习Java第二课</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="一、基本数据类型"><a href="#一、基本数据类型" class="headerlink" title="一、基本数据类型"></a>一、基本数据类型</h2><ul>
<li>整数类型 byte，short,int,long<ul>
<li>byte:-128~127</li>
<li>short:-32768~32767</li>
<li>int:-2147483648 ~ 2147483647</li>
<li>long: -9223372036854775808 ~ 9223372036854775807</li>
</ul>
</li>
<li>浮点数类型 float,double<ul>
<li>float:3.4x10^38</li>
<li>double:1.79x10^308</li>
</ul>
</li>
<li>字符类型 char<ul>
<li>字符使用<code>&#39;&#39;</code>表示</li>
<li>字符串使用<code>&quot;&quot;</code>表示，且需要<code>String</code>来表示</li>
</ul>
</li>
<li>布尔类型 boolean</li>
</ul>
<h2 id="二、三元运算符"><a href="#二、三元运算符" class="headerlink" title="二、三元运算符"></a>二、三元运算符</h2><ul>
<li><code>b?x:y</code><ul>
<li>会首先计算<code>b</code>，如果<code>b</code>为true，则只计算<code>x</code>，否则，只计算<code>y</code></li>
</ul>
</li>
</ul>
<h2 id="三、数组类型"><a href="#三、数组类型" class="headerlink" title="三、数组类型"></a>三、数组类型</h2><ul>
<li>例如定义一个整型数组<ul>
<li><code>int[] a = new int[3];</code> </li>
<li>当然我们可以不用这样写，直接赋值，就不用写数组长度，上述代码非可以写成这样<code>int[] a = new int[]{11,22,33};</code>或者这样<code>int[] a = {11,22,33};</code></li>
</ul>
</li>
<li>对于一个数组类型，我们可以使用<code>length</code>来获取数组的长度<ul>
<li>a.length; //结果为3</li>
</ul>
</li>
</ul>
<h2 id="四、程序的输入与输出"><a href="#四、程序的输入与输出" class="headerlink" title="四、程序的输入与输出"></a>四、程序的输入与输出</h2><ul>
<li>输出（相对输入容易一点）<ul>
<li><code>System.out.println(&quot;xxx&quot;);</code>这就是输出语句，当然在输出中我们还可以大做文章<ul>
<li>这个时候语句可以写成<code>System.out.printf(&quot;xxx&quot;);</code>注意看<code>print</code>变了，后面怎么玩，其实和C语言是差不多相通的</li>
</ul>
</li>
</ul>
</li>
<li>输入<ul>
<li>导入包<code>import java.util.Scanner;</code></li>
<li>新建一个对象<code>Scanner scanner = new Scanner(System.in);</code></li>
<li>读取输入<code>int a = scanner.nextInt();</code></li>
<li>上述三步才完成了基本的输入，第三部中有很多可以自己定义的地方，比如数据类型</li>
</ul>
</li>
</ul>
<h2 id="五、流程控制"><a href="#五、流程控制" class="headerlink" title="五、流程控制"></a>五、流程控制</h2><ul>
<li><p>if else</p>
<ul>
<li>这个结构其实和Python极为相似，但是不同的是，这里的if后面的语句必须有括号括起来，否则会报错,并且Java的<code>else if</code>连用是分开的，在python中可以用<code>elif</code>来表示<ul>
<li>eg 单个if使用  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">int a &#x3D; 8;</span><br><span class="line">if (a &gt;&#x3D; 5) &#123;</span><br><span class="line">    System.out.println(&quot;a&gt;5&quot;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>eg if和else连用  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">int a &#x3D; 8;</span><br><span class="line">if (a &gt;&#x3D; 15) &#123;</span><br><span class="line">    System.out.println(&quot;a&gt;&#x3D;15&quot;);</span><br><span class="line">&#125;else&#123;</span><br><span class="line">    System.out.println(&quot;a&lt;15&quot;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>eg if和else if连用  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">int a &#x3D; 8;</span><br><span class="line">if (a &gt;&#x3D; 15) &#123;</span><br><span class="line">    System.out.println(&quot;a&gt;&#x3D;15&quot;);</span><br><span class="line">&#125;else if (a &gt;&#x3D; 10)&#123;</span><br><span class="line">    System.out.println(&quot;10&lt;&#x3D;a&lt;15&quot;);</span><br><span class="line">&#125;else&#123;</span><br><span class="line">    System.out.println(&quot;a&lt;10&quot;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
</li>
<li><p>switch</p>
<ul>
<li>switch其实就是一个多重选择，在给定选项中选择对应数据插入程序，程序执行相应操作</li>
<li>一般语法结构  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">switch (option):</span><br><span class="line">case option1:</span><br><span class="line">    xxx;</span><br><span class="line">    break;</span><br><span class="line">case option2:</span><br><span class="line">    xxx;</span><br><span class="line">    break;</span><br><span class="line">...</span><br><span class="line">default:</span><br><span class="line">    xxx;</span><br><span class="line">    break;</span><br></pre></td></tr></table></figure></li>
<li>注意上述表示，<code>case</code>会枚举<code>option</code>中的每一种操作，当然<code>default</code>表示，若枚举的都没有，直接执行对应结果就好,关于<code>break</code>我们在后面讲解<ul>
<li>eg  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">import java.util.Scanner;</span><br><span class="line"></span><br><span class="line">public class LearnSwitch &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        Scanner num &#x3D; new Scanner(System.in);</span><br><span class="line">        int n &#x3D; num.nextInt();</span><br><span class="line">        num.close();</span><br><span class="line">        switch (n) &#123;</span><br><span class="line">        case 1:</span><br><span class="line">            System.out.println(&quot;1&quot;);</span><br><span class="line">            break;</span><br><span class="line">        case 2:</span><br><span class="line">            System.out.println(&quot;2&quot;);</span><br><span class="line">            break;</span><br><span class="line">        default:</span><br><span class="line">            System.out.println(&quot;no in switch!&quot;);</span><br><span class="line">            break;			</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
  该代码，若是输入1或者2就会被<code>case</code>捕捉到，执行相应语句，若没有捕捉到，则执行<code>default</code>语句。</li>
</ul>
</li>
</ul>
</li>
<li><p>while</p>
<ul>
<li>while循环语句</li>
<li>一般语法结构  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">while (条件)&#123;</span><br><span class="line">    需要执行的语句</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>比如我们执行1+2+3+…+100  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">public class Sum&#123;</span><br><span class="line">    public static void main(String[] args)&#123;</span><br><span class="line">        int n &#x3D; 1;</span><br><span class="line">        int sum &#x3D; 0;</span><br><span class="line">        while (n&lt;&#x3D;100)&#123;</span><br><span class="line">            sum +&#x3D; n;</span><br><span class="line">            n++;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(sum);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>do while</p>
<ul>
<li>和while相似，while是先判断再做，do while是先做再判断</li>
<li>一般语法结构  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">do &#123;</span><br><span class="line">    需要执行的语句</span><br><span class="line">&#125;while (条件);</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>for</p>
<ul>
<li>emmmm,这个就写个代码吧，和c语言基本一致  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">public class Sum &#123;</span><br><span class="line">public static void main(String[] args) &#123;</span><br><span class="line">    int sum &#x3D; 0;</span><br><span class="line">    for (int i&#x3D;1; i&lt;&#x3D;100; i++) &#123;</span><br><span class="line">        sum &#x3D; sum + i;</span><br><span class="line">    &#125;</span><br><span class="line">    System.out.println(sum);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>break &amp;&amp; continue</p>
<ul>
<li>他们两兄弟在循环中用的最多，也是最频繁</li>
<li>break会跳出当前循环</li>
<li>continue则是提前结束本次循环</li>
</ul>
</li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/05/29/Java/%E5%AD%A6%E4%B9%A0Java%E7%AC%AC%E4%BA%8C%E8%AF%BE/" data-id="ckarn4zs500053cbies54b7fw" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-Java/学习Java第三课" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/05/29/Java/%E5%AD%A6%E4%B9%A0Java%E7%AC%AC%E4%B8%89%E8%AF%BE/" class="article-date">
  <time datetime="2020-05-29T02:53:56.202Z" itemprop="datePublished">2020-05-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/05/29/Java/%E5%AD%A6%E4%B9%A0Java%E7%AC%AC%E4%B8%89%E8%AF%BE/">Java/学习Java第三课</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="一、面向对象"><a href="#一、面向对象" class="headerlink" title="一、面向对象"></a>一、面向对象</h2><p>嗯？还没有对象？来帮你new一个，嘿嘿嘿嘿，开玩笑呢！<br>面向对象的简单理解，就是给你一个菜谱，你对着菜谱做菜，你今天做，明天做，后天做，只要是按着这个菜谱来做的，就是按照他的规则来，菜谱就是模板，而做出来的菜就是实例，实例是按照模板出来的，不管如何，他总是和模板相似，这其实就是面向对象的一个过程。而面向对象的三大基本特点：封装、继承、多态，我们来一一理解。   </p>
<ul>
<li>封装<ul>
<li>简单来讲，封装就是一个模板，菜谱就是，它是高级大厨师封装出来的模板，给不会烧菜的小白做参考</li>
</ul>
</li>
<li>继承<ul>
<li>这点，可以看作是你用菜谱做菜，不管如何，你要用到里面加调料的步骤，这就是继承</li>
</ul>
</li>
<li>多态<ul>
<li>这个最好理解，就是你按照i菜谱做菜，可能真的是小白，第一次勉强做出来了，第二次…第无数次，可能每一次都不一样，而菜谱确是没有变的，这就是多态。</li>
</ul>
</li>
</ul>
<h2 id="二、一个小小的尝试"><a href="#二、一个小小的尝试" class="headerlink" title="二、一个小小的尝试"></a>二、一个小小的尝试</h2><ul>
<li>创建一个菜谱类Recipe，同时具有一些属性，分别为菜名<code>name</code>和菜的内容<code>content</code>  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">class Recipe&#123;</span><br><span class="line">    public String name;</span><br><span class="line">    Public String content;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>创建一个实例，炒菜(比如：xxx)  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Recipe xxx &#x3D; new Recipe();</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="三、关键内容"><a href="#三、关键内容" class="headerlink" title="三、关键内容"></a>三、关键内容</h2><p>刚刚我们尝试了一下，创建一个菜谱类，并包含两个字段，可以简单理解为类的属性，这里我们介绍定义在类中的方法，其实可以简单理解为C语言中的函数（ps:说的不是很谨慎，大概可以这样理解）</p>
<ul>
<li><p>方法</p>
  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">修饰符 方法返回类型 方法名(方法参数列表) &#123;</span><br><span class="line">    若干方法语句;</span><br><span class="line">    return 方法返回值;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>  关于修饰符，之前有提到过的<br>  <code>public    公共的  允许外部调用</code><br>  <code>private    私有的  不允许外部调用</code><br>  <code>protected    受保护的 不允许外部调用,允许子类调用</code></p>
</li>
<li><p>方法的传参<br>我们在定义方法时，通常需要考虑时候要给这个方法传递参数，这一点很重要，而且在设计完方法后，调用时要对应使用。</p>
  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public void setNameAndAge(String name, int age)&#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>  在使用这个方法时，务必要注意参数的类型</p>
<ul>
<li>可变参数 (用<code>类型...</code>定义)<br>关于这个可变参数，简单理解就是参数可以接受一个数组，从而实现可变参数,注意，务必将属性设置为数组，否则接收参数就会错误！  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public void setName(String... name)&#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>构造方法<br>不知你是否有过疑惑，刚刚实例化是怎么操作的？为什么可以这样？</p>
  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Recipe xxx &#x3D; new Recipe();</span><br></pre></td></tr></table></figure>
<p>  其本质原理是编译器会默认生成一个构造方法，其本质长这个样子</p>
  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">class Recipe&#123;</span><br><span class="line">    public String name;</span><br><span class="line">    Public String content;</span><br><span class="line"></span><br><span class="line">    public Recipe()&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>  那么，编译器会自动默认一个，我们可以自己写一个麻？答案是可以的，而且还可以写参数哦，这样就可以实现使用类实例化时就给对象赋一个初始值哦。(ps:若是改变了构造方法，编译器就不会自动创建一个，这样我们在使用中要注意若是我们改变构造方法，让其变为可以接受参数的构造方法，原本不传参数的构造方法就失效了哦，要使用也很简单，再写一遍不就好了，哈哈哈，看下面代码部分。)</p>
  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">class Recipe&#123;</span><br><span class="line">        public String name;</span><br><span class="line">        Public String content;</span><br><span class="line"></span><br><span class="line">        public Recipe()&#123;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        public Recipe(String name)&#123;</span><br><span class="line">            this.name &#x3D; name;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></li>
<li><p>方法的重载<br>通俗来说就是方法名相同，但各自所需传递的参数不同，从而实现不同的功能。</p>
</li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/05/29/Java/%E5%AD%A6%E4%B9%A0Java%E7%AC%AC%E4%B8%89%E8%AF%BE/" data-id="ckarn4zs200043cbi9hs964ol" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-失声痛哭之常用Linux命令" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/05/29/%E5%A4%B1%E5%A3%B0%E7%97%9B%E5%93%AD%E4%B9%8B%E5%B8%B8%E7%94%A8Linux%E5%91%BD%E4%BB%A4/" class="article-date">
  <time datetime="2020-05-29T02:53:56.202Z" itemprop="datePublished">2020-05-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/05/29/%E5%A4%B1%E5%A3%B0%E7%97%9B%E5%93%AD%E4%B9%8B%E5%B8%B8%E7%94%A8Linux%E5%91%BD%E4%BB%A4/">失声痛哭之常用Linux命令</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>事情的经过是这样的，为了写学校的下发的模板文件不会乱码，我在Linux下创建了一个Windows7的虚拟机，使用共享文档的方式在win系统下编辑我放在Ubuntu中的文件（ps：还好是这么做的，不然小命没了），可是我昨天再开机的时候，emmm，我的天，我的Ubuntu20.04进不去图形化界面了，emmm，那我虚拟机啥的都没法子用了啊，我的文件有一天的还没同步啊。哎，喂，这么可以这样…….<br>其实我在使用Ubuntu20.04的时候发现这个系统的图形化界面的操作是多么的不友好，虽然图形化界面相对18.04好看了太多，但是bug也太多了吧…..，软件开几个后就会出现死机的现象，按什么都没有反应，包括使用Ctrl+Alt+F3来使用命令行模式都不可以，只能重启电脑…我….还有这输入法，我用的难受，终于再使用大半个月后系统的图形化界面崩了，进不去了….（ps:还好终端的模式可以进去….）<br>下面就来讲解我怎么把数据备份出来的，以便大家参考   </p>
<ol>
<li>确认需要备份的文件及大小（这里有一个坑，后面会有提）<ul>
<li>在/home目录下，使用<code>df  -sh [用户名]</code>就可以查看这个用户目录下占用多大空间，我是52G…..我U盘才8G，这…</li>
<li>可能是我没有安装对应的支持，导致我在终端模式下，/home/bean/下的文件夹，中文都是乱码…..，我真的是无语，都不知道这么移动了，毕竟我U盘容量小了，不然就可以直接<code>sudo cp -rf * /U盘目录</code>，哎…</li>
</ul>
</li>
<li>准备一个大U盘，用来备份（越大越好）</li>
<li>挂载U盘，移动文件（还没结束哦！）<ul>
<li>使用<code>fdisk -l</code>查看U盘的盘符，使用<code>sudo mount /dev/u盘盘符  /mnt</code>就可以使用挂载命令将U盘挂载到<code>/mnt</code>目录下，直接使用<code>sudo cp -rf * /mnt</code>就可以了哦，小容量的自己想办法删些文件,没有乱码的可以在终端中删除，比如在Ubuntu的安装盘中使用试用安装模式来删除一些文件（我就是这样做的…）</li>
<li>卸载U盘<code>sudo umount /mnt</code>你以为这样就好了么，呵呵呵，后面还有呢</li>
</ul>
</li>
</ol>
<p>当我安装好18.04后（图形化界面确实比20.04强多了），我打开一看….都是<code>x</code>，没权限打开….，使用<code>ls -al或者ll</code>一看，用户为root，用户组为root….我们这个使用就要更改用户和用户组，命令分别是<code>chown</code>和<code>chgrp</code>,不过可以使用<code>chown</code>一个命令解决倒是，这里我将用户改为<code>bean</code>用户组改为<code>bean</code>，使用命令为<code>sudo chown -R bean:bean *</code>，这里的R表示同时改动当前文件夹下的子文件夹和文件，不然就得一个文件夹一个一个的改…..<br>我真的怕了，哎，哦对了，这篇文章就是使用我讲解hexo博客备份的方式来重新配置环境后再发出来的哦。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/05/29/%E5%A4%B1%E5%A3%B0%E7%97%9B%E5%93%AD%E4%B9%8B%E5%B8%B8%E7%94%A8Linux%E5%91%BD%E4%BB%A4/" data-id="ckarn4zqs00003cbi1ypzgjle" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  


  <nav id="page-nav">
    
    <span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><a class="extend next" rel="next" href="/page/2/">下一页 &amp;raquo;</a>
  </nav>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/06/">六月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/05/">五月 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2020/06/07/Python%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%93numpy%E7%9A%84%E7%AE%80%E5%8D%95%E5%85%A5%E9%97%A8%E4%BD%BF%E7%94%A8/">Python第三方库numpy的简单入门使用</a>
          </li>
        
          <li>
            <a href="/2020/06/02/Python%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%8F%8A%E7%94%9F%E6%88%90%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/">Python迭代器及生成器的简单理解</a>
          </li>
        
          <li>
            <a href="/2020/06/01/Python%E8%A3%85%E9%A5%B0%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/">Python装饰器的简单理解</a>
          </li>
        
          <li>
            <a href="/2020/06/01/Python%E9%97%AD%E5%8C%85%E7%9A%84%E7%AE%80%E8%A6%81%E4%BB%8B%E7%BB%8D/">闭包的简要介绍</a>
          </li>
        
          <li>
            <a href="/2020/05/31/Python%E8%BF%9E%E6%8E%A5C%E5%AE%9E%E7%8E%B0%E9%AB%98%E6%95%88%E7%BC%96%E7%A8%8Bdemo/">Python连接C实现高效编程demo</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>